{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "非参数估计--K近邻估计\n",
    "\n",
    "K近邻估计方法的思想是预先确定N的一个函数Kn，在x点近邻选择一个体积，使其不断增大直至捕获Kn个样本\n",
    "\n",
    "基本估计公式为：\n",
    "![K近邻](https://img-blog.csdn.net/20170613225949055)\n",
    "其中VN=hN^d,n代表样本容量。\n",
    "\n",
    "当满足一下三个条件时：\n",
    "当N→∞，VN=0,kN=∞,kN/VN=0时，p(X)收敛于真实分布。\n",
    "同时需要注意的是kN大小的选择会影响估计的结果，一般我们取kN=k1*√N。\n",
    "当样本容量N→∞时，最接近真实结果，所以我们一般取很大的N值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAa8AAADHCAYAAACuu9yCAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAA8p0lEQVR4nO2dd3xcxbX4v2d31bstyUUucje2ccGimo4xJuRhSkgooeRRQgIP8uOl54VASAiBQAolxAESSEIPLQkdbIrBgAs27pa7Lcuyei+7O78/7l15tVZZSVul8/1oPnvvzL1zz909mjP1jBhjUBRFUZR4whFtARRFURSlt6jxUhRFUeIONV6KoihK3KHGS1EURYk71HgpiqIocYcaL0VRFCXuUOOlKIqixB1qvHqJiOwUkTIRSfOLu0ZElvYynztE5AsRcYvIbaGWU4kukdITEckTkSdFpEZEqkTkH/2XXokkodAVEckXkadEpMTWhWUicqxf+qki4hWRer9wZUAeF4vIRhFpEJFtInJSSF4wTKjx6htO4OZ+5lEMfB/4T//FUWKUSOjJC0ApMAbIB37Tz+cp0aG/upIOfAbMBYYAjwP/EZF0v2tKjDHpfuFxX4KInAn8GvgGkAGcDGzvhzxhR41X37gH+K6IZPc1A2PM48a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cCbwBO4DFjzHoR+bmtoK8AjwJ/swfVK+1CCPu6Z7EG6N3ADaab2WLKwEJ1R1GUcNHjVPlYQERWmDidaquyR5d4fgeVXVG6Jl48bCyOtgD9QGWPLvH8Diq7onRBXLS8FEVRFMWfeGl5KYqiKEo7arwURVGUuCNujJeI3CYi+0Tkczt8Kdoy9URPTmljGRHZKSJf2N916B0GRgjVm8gyUPRGiX3iZsxLRG4D6o0xv4m2LMEQjFPaWEZEdgJFxph4XSQLqN5EmoGiN0rsEzctrzik3SmtMaYV8DmlVZTuUL1RlCCIN+N1o4isFZHHRCTWnbF15pT2MMeyMYwB3hSRlbbz23hG9SZyDCS9UWKYmDJeIvK2iKzrJCwC/ghMAGYD+4F7oynrIOBEY8xRwNnADSJycrQF6grVm5gibvRGiW9iwqu8D2PM/GCuE5E/A/8Oszj9pdeOZWMJY8w++7NMRF7E6s56P7pSdY7qTewQT3qjxDcx1fLqDhEZ4Xd6PrAuWrIEyWfAJBEZJyKJWD77XomyTEEhImkikuE7BhYQ+993p6jeRI6BpDdK7BNTLa8euFtEZmP1qe8EvhlVaXqgK6e0URYrWIYBL9obLrqAJ40xr0dXpD6jehM5BpLeKDFO3EyVVxRFURQfcdNtqCiKoig+1HgpiqIocYcaL0VRFCXuUOOlKIqixB1qvBRFUZS4Q42XoiiKEneo8VIURVHijv8P+v9kKVY6Zi4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 5 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def x_y_distance(x,y):\n",
    "    d = 0\n",
    "    dis = x - y\n",
    "    for z in dis:\n",
    "        d += z ** 2\n",
    "    distance = d**0.5\n",
    "    return distance\n",
    "\n",
    "def knn_estimate(n,x,d):\n",
    "    Kn = int(math.sqrt(len(n)))\n",
    "    Px = []\n",
    "    q = Kn/len(n)\n",
    "    for i in x:\n",
    "        hn = []\n",
    "        for j in n:\n",
    "            distance = x_y_distance(i,j)\n",
    "            hn.append(distance)\n",
    "        hn.sort()\n",
    "        if Kn == 1:\n",
    "            Vn = (hn[Kn-1] * 2) ** d\n",
    "        else:\n",
    "            Vn = (hn[Kn] * 2) ** d\n",
    "        Px.append(q/Vn)   #计算公式\n",
    "    return np.array(Px)\n",
    "\n",
    "def get_N(n):\n",
    "    nn = np.random.normal(0,1,n).reshape([-1,1])\n",
    "    return nn\n",
    "\n",
    "X = np.arange(-5, 5, 0.1).reshape([-1, 1])#reshape([-1,1])将矩阵转化为一列\n",
    "N = []\n",
    "result = []\n",
    "N_number = [1,16,256,10000,150000]\n",
    "for i in range(5):\n",
    "    N_i = get_N(N_number[i])\n",
    "    N.append(N_i)\n",
    "\n",
    "for i in range(5):\n",
    "    result_i = knn_estimate(N[i],X,d=1)\n",
    "    result.append(result_i)\n",
    "\n",
    "for i in range(1,6):\n",
    "    plt.subplot(3,3,i)\n",
    "    plt.plot(X,result[i-1])\n",
    "    plt.title('N={0}\\nKn={1}'.format(N_number[i-1],math.sqrt(N_number[i-1])))\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成均值为0，方差为1，长度为N的一维正态随机信号"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
